package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CaseInfo;
import cn.wolfcode.crm.domain.InsureInfo;
import cn.wolfcode.crm.mapper.CarMapper;
import cn.wolfcode.crm.domain.CompensationInfo;
import cn.wolfcode.crm.mapper.CaseInfoMapper;
import cn.wolfcode.crm.mapper.InsureInfoMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICaseInfoService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CaseInfoServiceImpl implements ICaseInfoService {
    @Autowired
    private CaseInfoMapper caseInfoMapper;

    @Autowired
    private InsureInfoMapper insureInfoMapper;

    @Autowired
    private CarMapper carMapper;

    private void save(CaseInfo entity){
        if (entity.getId() == null) {
            caseInfoMapper.insert(entity);
        } else {
            caseInfoMapper.updateByPrimaryKey(entity);
        }
    }

    public void saveOrUpdate(CaseInfo entity) {
        // 不存在的 id 就 插入
        CaseInfo old = null;
        if (entity.getId() != null) {
            old = caseInfoMapper.selectByPrimaryKey(entity.getId());
        }
        if (old == null) {
            entity.setId(null);
        } else {
            // update 杜绝 状态 已理赔 / 待审核 / 待验证
            Integer status = old.getStatus();
            if (CaseInfo.FINISH.equals(status) || CaseInfo.VERITY.equals(status) || CaseInfo.UNAUDIT.equals(status)) {
                throw new RuntimeException("该案件不允许修改");
            }
        }
        //通过编号insureInfoSn和车牌号carNum获取保单对象
        InsureInfo insureInfo = insureInfoMapper.getBySn(entity.getInsureInfoSn(), entity.getCarNum());
        //设置录入员
        entity.setInputUser(UserContext.getCurrentUser());
        //如果保单号或者车牌号两个其中一个不对,则设置状态为 0:没有该保单
        if (insureInfo == null || !InsureInfo.FINISH.equals(insureInfo.getState()) ) {
            entity.setStatus(CaseInfo.NOT_EXIST);
            save(entity);
            return;
        }
        //设置汽车的id,保单id,保单的sn
        if (insureInfo.getCar() != null) {
            entity.setCarId(insureInfo.getCar().getId());
        }
        entity.setInsureInfoId(insureInfo.getId());

        // 保单已经过止保日期 , 设置状态为2 :保单失效
        if (insureInfo.getEndDate().before(entity.getCaseTime())) {
            entity.setStatus(CaseInfo.TOO_LONG);
            save(entity);
            return;
        }
        //以上的条件都不成立 ,则设置状态为3 :待验证
        entity.setStatus(CaseInfo.UNVERITY);
        save(entity);
    }

    public void delete(Long id) {
        caseInfoMapper.deleteByPrimaryKey(id);
    }

    public CaseInfo get(Long id) {
        return caseInfoMapper.selectByPrimaryKey(id);
    }

    public List<CaseInfo> listAll() {
        return caseInfoMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = caseInfoMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<CaseInfo> caseInfos = caseInfoMapper.queryList(qo);
        return new PageResult(count, caseInfos);
    }

    // 验证案件
    @Override
    public void checkCase(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证 状态
        if (!CaseInfo.UNVERITY.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件处于 非待验证 状态");
        }
        // 设置 验证成功 验证员工 验证日期
        caseInfo.setStatus(CaseInfo.VERITY);
        caseInfo.setIdentifier(UserContext.getCurrentUser());
        caseInfo.setIdentifierTime(new Date());
        caseInfoMapper.checkCase(caseInfo);
    }

    // 提审案件
    public void needAudit(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证  并且 非拒审 状态
        if (!CaseInfo.VERITY.equals(caseInfo.getStatus()) && !CaseInfo.REJECT_AUDIT.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件的状态 不能提审");
        }
        // 设置 未审状态 提审员工
        caseInfo.setStatus(CaseInfo.UNAUDIT);
        caseInfo.setPayUser(UserContext.getCurrentUser());
        caseInfoMapper.needAudit(caseInfo);
    }

    // 拒审案件
    public void crash(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证 或 非 拒审 状态
        if (!CaseInfo.VERITY.equals(caseInfo.getStatus()) && !CaseInfo.REJECT_AUDIT.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件处于 不能废弃");
        }
        // 设置 审核状态 审核员工 审核日期
        caseInfo.setStatus(CaseInfo.CRASH);
        caseInfo.setAuditUser(UserContext.getCurrentUser());
        caseInfo.setAuditTime(new Date());
        caseInfoMapper.auditCase(caseInfo);
    }

    // 拒审案件
    public void cancelAudit(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证 状态
        if (!CaseInfo.UNAUDIT.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件处于 非待验证 状态");
        }
        // 设置 审核状态 审核员工 审核日期
        caseInfo.setStatus(CaseInfo.REJECT_AUDIT);
        caseInfo.setAuditUser(UserContext.getCurrentUser());
        caseInfo.setAuditTime(new Date());
        caseInfoMapper.auditCase(caseInfo);
    }
    // 审核案件
    public void auditCase(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证 状态
        if (!CaseInfo.UNAUDIT.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件处于 非待验证 状态");
        }
        // 设置 审核状态 审核员工 审核日期
        caseInfo.setStatus(CaseInfo.NOT_PAY);
        caseInfo.setAuditUser(UserContext.getCurrentUser());
        caseInfo.setAuditTime(new Date());
        caseInfoMapper.auditCase(caseInfo);
    }
    // 付款
    public void payCase(Long id) {
        CaseInfo caseInfo = caseInfoMapper.selectByPrimaryKey(id);
        // 杜绝 非带验证 状态
        if (!CaseInfo.NOT_PAY.equals(caseInfo.getStatus())) {
            throw new RuntimeException("该案件处于 非审核完 状态");
        }
        // 设置 审核状态 审核员工 审核日期
        caseInfo.setStatus(CaseInfo.FINISH);
        caseInfo.setPayMoneyUser(UserContext.getCurrentUser());
        caseInfo.setPayMoneyTime(new Date());
        caseInfoMapper.payMoneyCase(caseInfo);
    }
    //通过caseInfo的id查询CompensationInfo
    public CompensationInfo selectCompensationInfoById(Long caseInfoId) {
        return caseInfoMapper.selectCompensationInfoById(caseInfoId);
    }

}
